package com.wanglei.wc

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Spark01_wordcount {

  def main(args: Array[String]): Unit = {
    //
    val sparkConf = new SparkConf().setMaster("local[2]").setAppName("wc")
    val sc = new SparkContext(sparkConf)

    //
    val lines: RDD[String] = sc.textFile("datas")
    val words: RDD[String] = lines.flatMap(_.split(" "))
    val wordGroup: RDD[(String, Iterable[String])] = words.groupBy(word => word)
    val wordCount: RDD[(String, Int)] = wordGroup.map {
      case (word, list) => (word, list.size)
    }
    val res: Array[(String, Int)] = wordCount.collect()
    res.foreach(println(_))

    //
    sc.stop()
  }

}
